package com.kborid.musicplayer.Control;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

public abstract class ActivityBase extends Activity {

	private static final String TAG = "MenuControlIF";
	private static final String SUBTAG = "MenuBase: ";
	protected int m_iCurStatus = MENUBASE_STATUS.UNKNOWN;

	protected class MENUBASE_STATUS {
		public static final int UNKNOWN = 0;
		public static final int CREATE = 1;
		public static final int START = 2;
		public static final int RESUME = 3;
		public static final int PAUSE = 4;
		public static final int STOP = 5;
		public static final int DESTROY = 6;
	}

	@Override
	protected void finalize() throws Throwable {
		super.finalize();
	}

	protected void OnCreate(Bundle savedInstanceState) {
	}

	protected void OnResume() {
	}

	protected void OnPause() {
	}

	protected void OnStop() {
	}

	protected void OnDestroy() {
	}

	public boolean DispatchKeyEvent(KeyEvent ev) {
		return false;
	}

	public boolean OnKeyDown(int keyCode, KeyEvent event) {
		return false;
	}

	public boolean DispatchTouchEvent(MotionEvent ev) {
		return false;
	}

	public boolean OnTouchEvent(MotionEvent event) {
		return false;
	}

	public boolean DispatchTrackballEvent(MotionEvent ev) {
		return false;
	}

	@Override
	final protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, SUBTAG + "onCreate:" + this);
		m_iCurStatus = MENUBASE_STATUS.CREATE;
		OnCreate(savedInstanceState);
	}

	@Override
	final protected void onResume() {
		super.onResume();
		Log.d(TAG, SUBTAG + "onResume:" + this);
		m_iCurStatus = MENUBASE_STATUS.RESUME;
		OnResume();
	}

	@Override
	final protected void onPause() {
		Log.d(TAG, SUBTAG + "onPause:" + this);
		m_iCurStatus = MENUBASE_STATUS.PAUSE;
		OnPause();
		super.onPause();
	}

	@Override
	final protected void onStop() {
		Log.d(TAG, SUBTAG + "onStop:" + this);
		m_iCurStatus = MENUBASE_STATUS.STOP;
		OnStop();
		super.onStop();
	}

	@Override
	final protected void onDestroy() {
		Log.d(TAG, SUBTAG + "onDestroy:" + this);
		m_iCurStatus = MENUBASE_STATUS.DESTROY;
		OnDestroy();
		super.onDestroy();
	}

	@Override
	final public boolean dispatchKeyEvent(KeyEvent ev) {
		if (m_iCurStatus >= MENUBASE_STATUS.PAUSE) {
			return true;
		}
		if (ev.getKeyCode() == KEYCODE_ESCAPE) {
			ev = new KeyEvent(ev.getDownTime(), ev.getEventTime(),
					ev.getAction(), KeyEvent.KEYCODE_BACK, ev.getRepeatCount());
		}

		switch (ev.getKeyCode()) {
		case KeyEvent.KEYCODE_VOLUME_UP: {
			if (ev.getAction() == KeyEvent.ACTION_DOWN) {
			}
		}
			break;
		case KeyEvent.KEYCODE_VOLUME_DOWN: {
			if (ev.getAction() == KeyEvent.ACTION_DOWN) {
			}
		}
			break;
		default:
			break;
		}

		if (DispatchKeyEvent(ev)) {
			return true;
		} else {
			return super.dispatchKeyEvent(ev);
		}
	}

	@Override
	final public boolean dispatchTouchEvent(MotionEvent ev) {
		if (m_iCurStatus >= MENUBASE_STATUS.PAUSE) {
			return true;
		}

		if (DispatchTouchEvent(ev)) {
			return true;
		} else {
			return super.dispatchTouchEvent(ev);
		}
	}

	@Override
	final public boolean dispatchTrackballEvent(MotionEvent ev) {
		Log.d(TAG, "dispatchTrackballEvent[" + this + " " + ev.getAction()
				+ "]");
		if (m_iCurStatus >= MENUBASE_STATUS.PAUSE) {
			return true;
		}

		if (DispatchTrackballEvent(ev)) {
			return true;
		} else {
			return super.dispatchTrackballEvent(ev);
		}
	}

	private static final int KEYCODE_ESCAPE = 111;

	@Override
	final public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (m_iCurStatus >= MENUBASE_STATUS.PAUSE) {
			return true;
		}

		Log.d(TAG, SUBTAG + " onKeyDown[" + keyCode + " " + event.getAction()
				+ "]" + this);
		if (OnKeyDown(keyCode, event)) {
			return true;
		}

		switch (keyCode) {

		case KeyEvent.KEYCODE_BACK:
			if (event.getRepeatCount() == 0) {
			}
			return true;
		default:
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	final public boolean onTouchEvent(MotionEvent event) {
		if (m_iCurStatus >= MENUBASE_STATUS.PAUSE) {
			return true;
		}

		if (OnTouchEvent(event)) {
			return true;
		}
		return super.onTouchEvent(event);
	}
}
